package com.cn.lyl.lawyer.controller;

import com.cn.lyl.lawyer.model.entity.NewsTypeModel;
import com.cn.lyl.lawyer.model.vo.NewsTypeVo;
import com.cn.lyl.lawyer.service.NewsService;
import com.cn.lyl.lawyer.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/back/news")

public class NewsController {
    @Autowired
    private NewsService newsService;

    @PostMapping("/add")
    public Object add(NewsTypeVo newsTypeVo){
        newsService.add(newsTypeVo);
        return CommonResult.success(null);
    }
    @PostMapping("/changeHomeState")
    public Object changeHomeState(Integer id){
        newsService.changeHomeState(id);
        return CommonResult.success(null);
    }
    @PostMapping("/delete")
    public Object delete(Integer id){
        newsService.delete(id);
        return CommonResult.success(null);
    }

    @PostMapping("/edit")
    public Object edit(NewsTypeVo newsTypeVo){
        newsService.change(newsTypeVo);
        return CommonResult.success(null);
    }

    @GetMapping("/list")
    public Object findAll(NewsTypeVo newsTypeVo){
        List<NewsTypeVo> all = newsService.findAll(newsTypeVo);
        Map map = new HashMap();
        map.put("count",newsService.getCount(newsTypeVo));
        map.put("list",all);
        return CommonResult.success(map);
    }

    @GetMapping("/findInfo")
    public Object findById(Integer id){
        NewsTypeVo byId = newsService.findById(id);
        return CommonResult.success(byId);
    }

    @GetMapping("/type")
    public Object findRole(){
        List<NewsTypeModel> allType = newsService.findAllType();
        return CommonResult.success(allType);
    }

}
